package com.louis.resource;

import org.springframework.core.io.UrlResource;

import java.net.MalformedURLException;

/**
 * @author XRY
 * @date 2023年06月28日18:20
 */

//UrlResource访问网络资源
public class UrlResourceDemo {
    //http\file
    public static void loadUrlResource(String path){
        //创建Resource实现类的对象UrlResource
        try {
            UrlResource url = new UrlResource(path);
            //获取资源信息
            System.out.println("url.getURL()" + url.getURL());
            System.out.println("url.getFilename() = " + url.getFilename());
            System.out.println("url.getDescription() = " + url.getDescription());
            System.out.println("url.getInputStream() = " + url.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        //http前缀
//        UrlResourceDemo.loadUrlResource("http://www.baidu.com");
        /*
        url.getURL()http://www.baidu.com
        url.getFilename() =
        url.getDescription() = URL [http://www.baidu.com]
        url.getInputStream() = sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@7591083d
        * */

        //file前缀,下面的写法需要注意文件要放在根路径下（spring6),也可以写如绝对路径
        loadUrlResource("file:louis.txt");
        /*
        * url.getURL()file:louis.txt
        url.getFilename() = louis.txt
        url.getDescription() = URL [file:louis.txt]
        url.getInputStream() = java.io.BufferedInputStream@7eda2dbb
        * */
    }
}
